package org.goobs.mapping;

import java.util.HashMap;
import java.util.Map;

import org.goobs.database.Database;
import org.goobs.database.DatabaseRow;

public class Tag implements DatabaseRow{

	public static final String[] COLS = {"elem_id", "k", "v"};
	public static final String[] TYPES = {"INT", "VARCHAR(63)", "VARCHAR(63)"};
	public static final String[] INDEX = {"elem_id"};
	
	private int id;
	private String key, value;
	
	protected Tag(){
		
	}
	
	protected Tag(int elementID, String key, String value){
		recycle(elementID, key, value);
	}
	
	public int getElemId(){
		return id;
	}
	
	public String getKey(){
		return key;
	}
	
	public String getValue(){
		return value;
	}
	
	public void recycle(int elementID, String key, String value){
		this.id = elementID;
		if(key != null && key.length() > 63){
			key = key.substring(0, 63);
		}
		this.key = key;
		if(value != null && value.length() > 63){
			value = value.substring(0, 63);
		}
		this.value = value;
	}
	
	@Override
	public Map<String, Object> export() {
		Map <String, Object> export = new HashMap <String, Object> ();
		export.put("k", Database.sanitize(key));
		export.put("v", Database.sanitize(value));
		export.put("elem_id", id);
		return export;
	}

	@Override
	public void load(Map<String, Object> row) {
		id = (Integer) row.get("elem_id");
		key = (String) row.get("k");
		value = (String) row.get("v");
	}

}
